﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DBHelper;
using BusinessLayer.RegInfo.Entities;
using System.Data.SqlClient;
using System.Data;


namespace BusinessLayer.RegInfo
{
    public class AdminActionGroupBL
    {
        public List<AdminActionGroup> GetAll()
        {            
            var adapter = new BaseDAO(DbConfig.DbRegInfoConnectionString);
            var list = adapter.GetList<AdminActionGroup>(Common.Constants.StoredProcedures.GetAllAdminActionGroups, CommandType.StoredProcedure, CustomConnectionState.CloseOnExit);
            return list;
        }
        public int CreateAdminActionGroup(AdminActionGroup group)
        {
            int result = -1;
            try
            {
                var adapter = new BaseDAO(DbConfig.DbRegInfoConnectionString);
                adapter.AddParameter("@Code", group.Code);
                adapter.AddParameter("@Name", group.Name);
                result = adapter.ExecuteNonQuery(Common.Constants.StoredProcedures.InsertAdminActionGroup, CommandType.StoredProcedure, CustomConnectionState.CloseOnExit);
                return result;
            }
            catch (Exception ex)
            {

            }
            return result;


        }
        public int UpdateAdminActionGroup(AdminActionGroup group)
        {
            int result = -1;
            try
            {
                var adapter = new BaseDAO(DbConfig.DbRegInfoConnectionString);
                adapter.AddParameter("@ActionGroupID", group.ActionGroupID);
                adapter.AddParameter("@Code", group.Code);
                adapter.AddParameter("@Name", group.Name);

                result = adapter.ExecuteNonQuery(Common.Constants.StoredProcedures.UpdateAdminActionGroup, CommandType.StoredProcedure, CustomConnectionState.CloseOnExit);
                return result;
            }
            catch (Exception ex)
            {

            }
            return result;


        }

        public AdminActionGroup GetByCode(string code)
        {            
            var adapter = new BaseDAO(DbConfig.DbRegInfoConnectionString);
            adapter.AddParameter("@Code", code);
            var list = adapter.GetList<AdminActionGroup>(Common.Constants.StoredProcedures.GetAdminActionGroupByCode, CommandType.StoredProcedure, CustomConnectionState.CloseOnExit);
            return list.FirstOrDefault();
        }
        public AdminActionGroup GetByID(int id)
        {            
            var adapter = new BaseDAO(DbConfig.DbRegInfoConnectionString);
            adapter.AddParameter("@ActionGroupID", id);
            var list = adapter.GetList<AdminActionGroup>(Common.Constants.StoredProcedures.GetAdminActionGroupByID, CommandType.StoredProcedure, CustomConnectionState.CloseOnExit);
            return list.FirstOrDefault();
        }
        public int DeleteAdminActionGroupByIds(List<string> lstofids)
        {
            int result = -1;

            string ids = "";
            foreach (var id in lstofids)
            {
                ids += id + ",";
            }
            ids = String.IsNullOrEmpty(ids) ? "" : ids.Substring(0,ids.Length-1);
            if (!String.IsNullOrEmpty(ids))
            {
                try
                {
                    var adapter = new BaseDAO(DbConfig.DbRegInfoConnectionString);
                    result = adapter.ExecuteNonQuery(String.Format("DELETE FROM AdminActionGroups WHERE ActionGroupID IN ({0});", ids), CommandType.Text, CustomConnectionState.CloseOnExit);
                    return result;
                }
                catch (Exception ex)
                {

                }
            }
            return result;
        }
        
    }
}
